
package com.example.ventilation.widget.MPChart.chart;

import android.content.Context;
import android.widget.TextView;

import com.example.ventilation.R;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.MarkerView;
import com.github.mikephil.charting.data.CandleEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.utils.MPPointF;
import com.github.mikephil.charting.utils.Utils;

import java.util.List;

/**
 *  MarkerView. 点击显示的
 *
 * @author Philipp Jahoda
 * https://javadoc.jitpack.io/com/github/PhilJay/MPAndroidChart/v3.1.0/javadoc/
 */
public class MyMarkerView extends MarkerView {

    private TextView tvContent;
    private TextView textView_x;
    private Chart lineChart;
    private List<String> format_realtimes;
    public MyMarkerView(Context context, int layoutResource,Chart lineChart,List<String> format_realtimes) {
        super(context, layoutResource);

        tvContent = findViewById(R.id.tvContent);
        textView_x = findViewById(R.id.tvContent_x);
        this.lineChart = lineChart;
        this.format_realtimes = format_realtimes;
    }

    // callbacks everytime the MarkerView is redrawn, can be used to update the
    // content (user-interface)
    @Override
    public void refreshContent(Entry e, Highlight highlight) {

        if (e instanceof CandleEntry) {

            CandleEntry ce = (CandleEntry) e;

            tvContent.setText("" + Utils.formatNumber(ce.getHigh(), 1, true));
        } else {
          //  textView_x.setText(""+e.getX());
             if (format_realtimes !=null && format_realtimes.size()>0 ){//非空判断
                 if ((int)e.getX() >= 0 && format_realtimes.get((int)e.getX())!= null &&format_realtimes.get((int)e.getX()).length()>0)//非空判断;
                   textView_x.setText("x="+format_realtimes.get((int)e.getX()));
             }


            tvContent.setText("y=" + Utils.formatNumber(e.getY(), 1, false));//第三个参数:以千分位分隔 000,000,000这种;

        }

        super.refreshContent(e, highlight);
    }

    @Override
    public MPPointF getOffset() {
        return new MPPointF(-(getWidth() / 2), -getHeight());
    }
}
